package doorwifi.wd.patientsation.peripheral.mainapp.fragment;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.LayoutParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import java.util.ArrayList;

import doorwifi.wd.patientsation.peripheral.mainapp.R;
import doorwifi.wd.patientsation.peripheral.mainapp.utils.UIUtils;

/**
 * Created by xuhuan on 2017/7/6.
 * 广播
 */

public class Broadcast extends Fragment implements View.OnClickListener {

    @ViewInject(R.id.broadcast_rg)
    private RadioGroup broadcast_rg;
    @ViewInject(R.id.all_broadcast_rb)
    private RadioButton all_broadcast_rb;
    @ViewInject(R.id.timing_broadcast_rb)
    private RadioButton timing_broadcast_rb;
    private ArrayList<Fragment> fragmentList;
    private allBroadcast allBroadcast;
    private timingBroadcast timingBroadcast;
    private FragmentManager fragmentManager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.xh_broadcast_fragment_layout, null);
        x.view().inject(this, view);
        initView();
        return view;
    }

    private void initView() {
        fragmentManager = getFragmentManager();
        setTabSelection(0);
        all_broadcast_rb.setOnClickListener(this);
        timing_broadcast_rb.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.all_broadcast_rb:
                setTabSelection(0);
                break;
            case R.id.timing_broadcast_rb:
                setTabSelection(1);
                break;
        }
    }


    private void setTabSelection(int index) {

//        all_broadcast_rb.setBackgroundColor(Color.WHITE);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index) {
            case 0:
                if (allBroadcast == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    allBroadcast = new allBroadcast();
                    transaction.add(R.id.broadcast_realtabcontent, allBroadcast);

                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(allBroadcast);
                }
                break;
            case 1:
                if (timingBroadcast == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    timingBroadcast = new timingBroadcast();
                    transaction.add(R.id.broadcast_realtabcontent, timingBroadcast);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(timingBroadcast);
                }
                break;

        }
        transaction.commit();
    }
    private void hideFragments(FragmentTransaction transaction) {
        if (allBroadcast != null) {
            transaction.hide(allBroadcast);
        }
        if (timingBroadcast != null) {
            transaction.hide(timingBroadcast);
        }

    }
}
